/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-06-05     sethnie       the first version
 */
#include <rtthread.h>
#include "lcd_st7789.h"
#include "HAL_OV2640.h"
void LCD_Reset_GPIO_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_SetBits(GPIOA, GPIO_Pin_15);
}
void DMA_SRAMLCD_Init(u32 ddr)
{
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
    DMA_DeInit(DMA2_Channel5);

    DMA_InitTypeDef DMA_InitStructure = {0};
    DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)LCD_DATA;
    DMA_InitStructure.DMA_MemoryBaseAddr = ddr;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
    DMA_InitStructure.DMA_BufferSize = LCD_W;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
    DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
    DMA_InitStructure.DMA_M2M = DMA_M2M_Enable;
    DMA_Init(DMA2_Channel5, &DMA_InitStructure);
}
void DMA_SRAMLCD_Enable(void)
{
    DMA_Cmd(DMA2_Channel5, DISABLE);
    DMA_SetCurrDataCounter(DMA2_Channel5, LCD_W);
    DMA_Cmd(DMA2_Channel5, ENABLE);
}
rt_bool_t st7789_init()
{
    /* LCD reset */
       LCD_Reset_GPIO_Init();
       GPIO_ResetBits(GPIOA, GPIO_Pin_15);
       rt_thread_mdelay(100);
       GPIO_SetBits(GPIOA, GPIO_Pin_15);
       LCD_Init();
       LCD_SetBrightness(75);
       LCD_SetColor(0x18E3, RED);
       LCD_DrawRectangle(40, 80, 200, 160,1);
       LCD_ShowString(64, 104, 32, 0, "WAITING");

       /* Camera init */
       ov2640_init();

       /* Video data path */
       LCD_AddressSetWrite(0, 0, LCD_W - 1, LCD_H - 1);
       DMA_SRAMLCD_Init((u32)get_RGB565_DVPDMAaddr0()); // DMA2
       DVP_Init();
       rt_kprintf("DVP Enable\r\n");

}
/* 导出到 msh 命令列表中 */
INIT_APP_EXPORT(st7789_init);


